home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1175 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: ornews.intel.com!news
  2. From: thurman_b_miller@ccm2.hf.intel.com (Thurman Miller)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: newbie need help with pointers
  5. Date: Tue, 09 Jan 1996 18:07:56 GMT
  6. Organization: Intel Corporation
  7. Message-ID: <4cub0b$rml@ornews.intel.com>
  8. References: <4cse34$308@news.iconn.net>
  9. NNTP-Posting-Host: thurman-pc.ssd.intel.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. thecrow@iconn.net (The Crow) wrote:
  13.  
  14. >Hey,
  15. >    Ok, I understand what pointers *are*, I know how to declare one, how to 
  16. >use them with simple things and sometimes how to use them to do dynamic memory 
  17. >allocation.  I don't feel very comfortable with them yet, nor have I seen any 
  18. >practical example of where they are useful *except* for dynamic memory 
  19. >allocation and in functions...and in arrays, but I am not very comfortable in 
  20. >any of these uses anyway, and can't think of a real problem where it would be 
  21. >advantageous or neccesary to use them. I have also had some trouble using them 
  22. >with STRUCTURE arrays (havent tried classes yet but I assume I will run into 
  23. >similar problems) 
  24.  
  25. >    If anyone has a good idea for a programming exercise that would get me 
  26. >familiar with pointers tell me.  My tutorial's exercises are way too easy. If 
  27. >it was an interesting one that would be nice, but just so I get comfortable 
  28. >with them.  I don't really know them well enough to think of  a good exercise 
  29. >myself, or I would.  Thanks for the help in advance.  Please reply via email, 
  30. >this newsgroup grows too fast. Thanks.
  31.  
  32. >-- 
  33. >The Crow - thecrow@iconn.net
  34. >"It can't rain all the time"
  35. >-Kryptology
  36.  
  37. The main reasons for using pointers are so that you don't pass an
  38. entire structure to a function so it can use it. Strings are a good
  39. example in that you don't want to pass the entire string to a
  40. function, just a pointer to it's location.
  41.  
  42. If the function is planning on updating the value you are passing,
  43. then you should use a reference to the variable (&) instead of a
  44. pointer.
  45.  
  46. As for some sample tutorials to get used to pointers, try to replace
  47. any parameters that are large objects (arrays, linked lists, strings)
  48. by defining a pointer to that type of object and setting that pointer
  49. to the object and then passing the pointer in the function.
  50.  
  51. Oh, just thought of a great example of pointers, write some code using
  52. an array and then change it to be a linked list. You'll need member
  53. functions GetFirst, GetNext, GetPrev, etc and they should all take
  54. pointers as arguments (pass in the pointer to the current item and
  55. they return a pointer to the item in question)
  56.  
  57. Good luck!
  58.  
  59. Thurman
  60.  
  61.  
  62.  
  63.